<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\common\HttpCurl;
use app\models\Category;
use app\models\CustomerService;
use app\models\Goods;
use app\models\GoodsCategory;
use app\models\GoodsCategoryCopy;
use app\models\GoodsCopy;
use app\models\GoodsSku;
use app\models\GoodsSkuCopy;
use app\models\Order;
use app\models\Price;
use app\models\Stock;
use yii;


class GoodsLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getAllGoods(){
        return Goods::find()->asArray()->all();
    }


    public  function getGoodsByYx($arr){
        return Goods::find()->where(['yx_goods_id'=>$arr])->asArray()->all();
    }
    public  function getGoodsByYxOne($yx_goods_id){
        return Goods::find()->where(['yx_goods_id'=>$yx_goods_id])->asArray()->one();
    }


    public function getByYxId($yx_id){
        if($goods = Goods::findOne(['yx_goods_id'=>$yx_id])){
            return $goods->toArray();
        }else{
            return false;
        }
    }

    public function addGoods($goodstemp,$params,$sku_stock=[]){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $goods = new Goods();
        $goods->yx_goods_id = $goodstemp['id'];
        $goods->goods_name = $goodstemp['name'];
        $time = date('Y-m-d H:i:s');
        $goods->add_time = $time;
        $goods->update_time = $time;
        $goods->simple_desc = $goodstemp['simpleDesc'];
        $goods->attr_list = json_encode($goodstemp['attrList']);
        $goods->goods_detail = $goodstemp['itemDetail']['detailHtml'];
        $goods->goods_pic = HttpCurl::download($goodstemp['listPicUrl']);
        /*if($params['goods_sort'][$goodstemp['id']])
            $goods->goods_sort = $params['goods_sort'][$goodstemp['id']];
        else
            $goods->goods_sort = 0;
        if($params['is_hot'][$goodstemp['id']])
            $goods->is_hot = 1;
        else
            $goods->is_hot = 0;*/
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl1'])){
            $goods->goods_pics = HttpCurl::download($goodstemp['itemDetail']['picUrl1']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl2'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl2']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl3'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl3']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl4'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl4']);
        }
        //$goods->goods_pics = HttpCurl::download($goodstemp['itemDetail']['picUrl1']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl2']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl3']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl4']);
        $result1 = $goods->save();
        $result2 = true;
        $result3 = true;

        if(isset($goodstemp['categoryPathList']) && $goodstemp['categoryPathList']){
            foreach($goodstemp['categoryPathList'] AS $value){
                if(isset($value[0])){
                    /*$category = Category::findOne(['category_name'=>$value[0]['name']])->toArray();
                    //$goodsCategory = GoodsCategory::findOne(['goods_id'=>$result1->goods_id,'category_id'=>$category['category_id']])
                    $goodsCategory = new GoodsCategory();
                    $goodsCategory->goods_id = $goods->goods_id;
                    $goodsCategory->first_category_id = $category['category_id'];
                    if(isset($value[1])){
                        $category = Category::find()->where(['category_name'=>$value[1]['name']])->asArray()->one();
                        if($category)
                            $goodsCategory->second_category_id = $category['category_id'];
                    }*/
                    $category = Category::find()->where(['yx_category_id'=>$value[0]['id']])->asArray()->one();
                    $goodsCategory = new GoodsCategory();
                    $goodsCategory->goods_id = $goods->goods_id;
                    $goodsCategory->first_category_id = $category['category_id'];
                    if(isset($value[1])){
                        $subcategory = Category::find()->where(['yx_category_id'=>$value[1]['id']])->asArray()->one();
                        $goodsCategory->second_category_id = $subcategory['category_id'];
                    }
                    $result2 &= $goodsCategory->save();
                }
            }
        }

        foreach($goodstemp['skuList'] AS $v){
            $goodsSku = new GoodsSku();
            $goodsSku->yx_sku_id = $v['id'];
            $goodsSku->goods_id = $goods->goods_id;
            $goodsSku->sku_price = $params['sku_price'][$goodstemp['id']][$v['id']]?$params['sku_price'][$goodstemp['id']][$v['id']]:$v['yanxuanPrice'];
            $goodsSku->yanxuan_price = $v['yanxuanPrice'];
            $goodsSku->channel_price = $v['channelPrice'];
            $goodsSku->sku_pic = HttpCurl::download($v['picUrl']);
            $goodsSku->display_string = $v['displayString'];
            $sku_spec = [];
            foreach($v['itemSkuSpecValueList'] AS $kk=>$vv){
                $sku_spec[] = ['spec_name'=>$vv['skuSpec']['name'],'spec_value'=>$vv['skuSpecValue']['value']];
            }
            $goodsSku->sku_spec = json_encode($sku_spec);
            $sku_price[] = $goodsSku->sku_price;
            $yanxuan_price[] = $goodsSku->yanxuan_price;

            foreach($sku_stock AS $vvv){
                if($vvv['skuId'] ==$v['id'])
                    $goodsSku->inventory = $vvv['inventory'];
            }

            $result3 &= $goodsSku->save();
        }
        $goods_price = min($sku_price);
        $yanxuan_price = min($yanxuan_price);
        if($result1 && $result2 && $result3){
            $goods->goods_price = $goods_price;
            $goods->yanxuan_price = $yanxuan_price;
            $goods->save();
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }

    }

    public function getOnGoods($yx_goods_id,$query = [], $page = 1, $limit = 10)
    {
        $goods = Goods::find()
            ->select('g.*,c1.category_name as first_category_name,c2.category_name as second_category_name')
            //->select('g.*,group_concat(c1.category_name) as first_category_name, group_concat(c2.category_name) as second_category_name')

            ->from('{{%goods}} g')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            /*->from('{{%goods_category}} gc')
            ->leftJoin('{{%goods}} g','gc.goods_id=g.goods_id')*/
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where("g.status=1");
        if(isset($query['spuid'])  && $query['spuid']!=""){
            $goods->andWhere(['yx_goods_id'=>$query['spuid']]);
        };
        if(isset($query['goods_name']) && $query['goods_name']!=""){
            $goods->andWhere(['LIKE','goods_name',$query['goods_name']]);
        };
        $second_category = '';
        if(isset($query['first_category']) && $query['first_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $second_category = $this->getCategory($query['first_category'],2);
        };

        if(isset($query['second_category']) && $query['second_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $goods->andWhere(['gc.second_category_id'=>$query['second_category']]);
        };

        if(isset($query['ishotornew']) && $query['ishotornew']!=0){
            if($query['ishotornew']==1)
                $goods->andWhere(['g.is_hot'=>1]);
            elseif($query['ishotornew']==2)
                $goods->andWhere(['g.is_new'=>1]);
        };
        if(isset($query['can_buy']) && $query['can_buy']!=-1){
            $httpcurl = new HttpCurl();
            $result = $httpcurl->rollpost("yanxuan.item.id.batch.get");
            if($query['can_buy']==1){
                $goods->andWhere(['g.yx_goods_id'=>$yx_goods_id]);
            }else{
                $goods->andWhere(['not in', 'g.yx_goods_id',$yx_goods_id]);
            }
        }
        $goods->orderBy('goods_id desc');
        $count = $goods->count();
        $lists = $goods->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();

        foreach($lists AS &$value){
            if(in_array($value['yx_goods_id'],$yx_goods_id)){
                $value['can_buy'] = 1;
            }else{
                $value['can_buy'] = 0;
            }
        }
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/on',
                'query' => $query
            ],
            'second_category'=>$second_category
        ];
        return $return;
    }


    public function getGoods($yx_goods_id,$query = [], $page = 1, $limit = 10)
    {
        $goods = Goods::find()
            ->select('g.*,c1.category_name as first_category_name,c2.category_name as second_category_name')
            //->select('g.*,group_concat(c1.category_name) as first_category_name, group_concat(c2.category_name) as second_category_name')

            ->from('{{%goods}} g')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            /*->from('{{%goods_category}} gc')
            ->leftJoin('{{%goods}} g','gc.goods_id=g.goods_id')*/
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where("g.buy_status=1");
        if(isset($query['spuid'])  && $query['spuid']!=""){
            $goods->andWhere(['yx_goods_id'=>$query['spuid']]);
        };
        if(isset($query['goods_name']) && $query['goods_name']!=""){
            $goods->andWhere(['LIKE','goods_name',$query['goods_name']]);
        };
        $second_category = '';
        if(isset($query['first_category']) && $query['first_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $second_category = $this->getCategory($query['first_category'],2);
        };

        if(isset($query['second_category']) && $query['second_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $goods->andWhere(['gc.second_category_id'=>$query['second_category']]);
        };
        if(isset($query['status']) && $query['status']!=-1){
            $goods->andWhere(['g.status'=>$query['status']]);
        }
        $goods->orderBy('goods_id desc');
        $count = $goods->count();
        $lists = $goods->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();

        /*foreach($lists AS &$value){
            if(in_array($value['yx_goods_id'],$yx_goods_id)){
                $value['can_buy'] = 1;
            }else{
                $value['can_buy'] = 0;
            }
        }*/

        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/yanxuan',
                'query' => $query
            ],
            'second_category'=>$second_category
        ];
        return $return;
    }

    public function getCategory($parent_id=0,$level=1){
        return Category::find()->where(['parent_id'=>$parent_id,'level'=>$level])->asArray()->all();
    }

    public function getById($id)
    {
        return Goods::find()->where(['goods_id' => $id])->asArray()->one();
    }

    public function getSkuById($id)
    {
        return GoodsSku::find()->where(['goods_id' => $id])->asArray()->all();
    }

    public function getCategoryById($id)
    {
        return GoodsCategory::find()
            ->select('c1.category_name as first_category_name, c2.category_name as second_category_name')
            ->from('{{%goods_category}} gc')
            //->leftJoin('{{%goods}} g','gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where(['goods_id' => $id])->asArray()->all();
    }

    public function off($id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $time = date('Y-m-d H:i:s');
        $goods = Goods::findOne($id);
        $goodsCopy = new GoodsCopy();
        foreach($goods->getOldAttributes() AS $key=>$value){
            if($key!='status' && $key!='update_time' && $key!='buy_status' && $key!='buy_status')
                $goodsCopy->setAttribute($key,$value);
        }
        unset($goodsCopy->goods_id);

        $goodsCopy->add_time = $time;
        $result1 = $goodsCopy->save();
        $goods->status = 0;
        $result2 = $goods->save();
        $result3 = true;
        $result4 = true;
        $result5 = true;
        $result6 = true;

        $goods_category = GoodsCategory::findAll(['goods_id'=>$id]);
        foreach($goods_category AS $value){
            $goodsCategoryCopy = new GoodsCategoryCopy();
            //$goodsCategoryCopy = $value;
            foreach($value->getOldAttributes() AS $ke=>$va){
                $goodsCategoryCopy->setAttribute($ke,$va);
            }
            unset($goodsCategoryCopy->goods_category_id);
            $goodsCategoryCopy->goods_id = $goodsCopy->goods_id;
            $result3 &= $goodsCategoryCopy->save();
            //$result4 &= $value->delete();
        }


        $goods_sku = GoodsSku::findAll(['goods_id'=>$id]);
        foreach($goods_sku AS $value){
            $goodsSkuCopy = new GoodsSkuCopy();
            foreach($value->getOldAttributes() AS $ke=>$va){
                if($ke!='sku_sales_volume' && $ke!='inventory' && $ke!='inventory_status')
                    $goodsSkuCopy->setAttribute($ke,$va);
            }
            //$goodsSkuCopy = $value;
            unset($goodsSkuCopy->sku_id);
            $goodsSkuCopy->goods_id = $goodsCopy->goods_id;
            $result5 &= $goodsSkuCopy->save();
            //$result6 &= $value->delete();
        }


        if($result1 && $result2 && $result3 /*&& $result4*/ && $result5 /*&& $result6*/){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }

    }


    public function getOffGoods($query = [], $page = 1, $limit = 10)
    {
        //$goods = GoodsCategoryCopy::find()
        $goods = GoodsCopy::find()
            ->select('g.*,c1.category_name as first_category_name,c2.category_name as second_category_name')
            //->select('g.*,group_concat(c1.category_name) as first_category_name, group_concat(c2.category_name) as second_category_name')

            ->from('{{%goods_copy}} g')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            /*->from('{{%goods_category_copy}} gc')
            ->leftJoin('{{%goods_copy}} g','gc.goods_id=g.goods_id')*/
//            ->leftJoin('{{%goods}} gcopy','gcopy.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where("1=1");
        if(isset($query['spuid'])  && $query['spuid']!=""){
            $goods->andWhere(['g.yx_goods_id'=>$query['spuid']]);
        };
        if(isset($query['goods_name']) && $query['goods_name']!=""){
            $goods->andWhere(['LIKE','g.goods_name',$query['goods_name']]);
        };
        $second_category = '';
        if(isset($query['first_category']) && $query['first_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $second_category = $this->getCategory($query['first_category'],2);
        };

        if(isset($query['second_category']) && $query['second_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $goods->andWhere(['gc.second_category_id'=>$query['second_category']]);
        };
        $goods->orderBy('g.goods_id desc');

        $lists = $goods->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
//var_dump($lists);exit;
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $goods->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/off',
                'query' => $query
            ],
            'second_category'=>$second_category
        ];
        return $return;
    }


    public function saveStatusByYx($yx_goods_id,$status,$params)
    {
        $goods = Goods::findOne(['yx_goods_id'=>$yx_goods_id]);
        if($goods->buy_status==0){
            return false;
        }
        $goods->status = $status;
        /*$goods->is_hot = $params['is_hot'][$yx_goods_id];
        if($params['goods_sort'][$yx_goods_id])
            $goods->goods_sort = $params['goods_sort'][$yx_goods_id];
        else
            $goods->goods_sort = 0;*/
        $goods->update_time = date('Y-m-d H:i:s');
        return $goods->save();
    }

    public function getOffById($id)
    {
        return GoodsCopy::find()->where(['goods_id' => $id])->asArray()->one();
    }

    public function getOffSkuById($id)
    {
        return GoodsSkuCopy::find()->where(['goods_id' => $id])->asArray()->all();
    }

    public function getOffCategoryById($id)
    {
        return GoodsCategoryCopy::find()
            ->select('c1.category_name as first_category_name, c2.category_name as second_category_name')
            ->from('{{%goods_category_copy}} gc')
            //->leftJoin('{{%goods}} g','gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where(['goods_id' => $id])->asArray()->all();
    }

    public function delOffById($goods_id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $goods = GoodsCopy::findOne($goods_id);
        $result1 = $goods->delete();
        $goodsCategory = GoodsCategoryCopy::findOne(['goods_id'=>$goods_id]);
        $result2 = $goodsCategory->delete();
        $goodsSku = GoodsSkuCopy::findOne(['goods_id'=>$goods_id]);
        $result3 = $goodsSku->delete();
        if($result1 && $result2 && $result3){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function getSkuByYxid($yx_goods_id){
        return GoodsSku::find()
            ->from('{{%goods_sku}} gs')
            ->leftJoin('{{%goods}} g','gs.goods_id=g.goods_id')
            ->where(['g.yx_goods_id'=>$yx_goods_id])
            ->asArray()->all();
    }

    public function getGoodsByYxAndCate($arr)
    {
        return Goods::find()
            ->select('g.*,c1.category_name as first_category_name,c2.category_name as second_category_name')
            ->from('{{%goods}} g')
            ->leftJoin('{{%goods_category}} gc', 'gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1', 'c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2', 'c2.category_id=gc.second_category_id')
            ->where(['g.yx_goods_id' => $arr])->asArray()->one();
    }


    public function getGoodsByYxAndAllCate($yx_goods_id){
            return GoodsCategory::find()
                ->select('gc.*,c1.level as first_level,c1.category_name as first_category_name,c2.level as second_level,c2.category_name as second_category_name')
                ->from('{{%goods_category}} gc')
                ->leftJoin('{{%goods}} g', 'gc.goods_id=g.goods_id')
                ->leftJoin('{{%category}} c1', 'c1.category_id=gc.first_category_id')
                ->leftJoin('{{%category}} c2', 'c2.category_id=gc.second_category_id')
                ->where(['g.yx_goods_id'=>$yx_goods_id])->asArray()->all();

    }

    public function recommend($goods_id,$goods_ishotornew,$goods_sort){
        $goods = Goods::findOne($goods_id);
        if($goods_ishotornew==1){
            $goods->is_hot = 1;
            $goods->is_new = 0;
        }elseif($goods_ishotornew==2){
            $goods->is_hot = 0;
            $goods->is_new = 1;
        }
        $goods->goods_sort = $goods_sort;
        return $goods->save();
    }

    public function offRecommend($id){
        $goods = Goods::findOne($id);
        $goods->is_hot = 0;
        $goods->is_new = 0;
        return $goods->save();
    }

    public function getOnGoodsSku($query = [], $page = 1, $limit = 100){
        $goods = GoodsSku::find()
            ->select('gs.*,g.yx_goods_id,g.goods_name,c1.category_name as first_category_name,c2.category_name as second_category_name')
            //->select('g.*,group_concat(c1.category_name) as first_category_name, group_concat(c2.category_name) as second_category_name')

            ->from('{{%goods_sku}} gs')
            ->leftJoin('{{%goods}} g','gs.goods_id=g.goods_id')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where("g.buy_status=1");
        if(isset($query['spuid'])  && $query['spuid']!=""){
            $goods->andWhere(['yx_goods_id'=>$query['spuid']]);
        };

        if(isset($query['skuid'])  && $query['skuid']!=""){
            $goods->andWhere(['yx_sku_id'=>$query['skuid']]);
        };
        if(isset($query['goods_name']) && $query['goods_name']!=""){
            $goods->andWhere(['LIKE','goods_name',$query['goods_name']]);
        };
        $second_category = '';
        if(isset($query['first_category']) && $query['first_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $second_category = $this->getCategory($query['first_category'],2);
        };

        if(isset($query['second_category']) && $query['second_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $goods->andWhere(['gc.second_category_id'=>$query['second_category']]);
        };
        if(isset($query['status']) && $query['status']!=-1){
            $goods->andWhere(['g.status'=>$query['status']]);
        }

        if(isset($query['ishotornew']) && $query['ishotornew']!=0){
            if($query['ishotornew']==1)
                $goods->andWhere(['g.is_hot'=>1]);
            elseif($query['ishotornew']==2)
                $goods->andWhere(['g.is_new'=>1]);
        };

        $goods->orderBy('goods_id desc');
        $count = $goods->count();
        $lists = $goods->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();


        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/goods-sku',
                'query' => $query
            ],
            'second_category'=>$second_category
        ];
        return $return;
    }

    public function addAudit($skuids,$price_type,$price_base,$price_model,$price_count){
        $price = new Price();
        $userinfo = Yii::$app->session->get('user');
        $price->admin_id = $userinfo['uid'];
        $price->add_time = date('Y-m-d H:i:s');
        $price->price_type = $price_type;
        if($price_base!==null)
        $price->price_base = $price_base;
        if($price_model!==null)
        $price->price_model = $price_model;
        $price->price_count = $price_count;
        $price->skuids = $skuids;
        $price->save();
    }

    public function getPriceAudit($query = [], $page = 1, $limit = 10){
        $price = Price::find()
            ->select('p.*,a.sex_name')
            ->from('{{%price}} p')
            ->leftJoin('{{%admin}} a','a.id=p.admin_id');
        if(isset($query['audit_status'])  && $query['audit_status']!=-1){
            $price->andWhere(['audit_status'=>$query['audit_status']]);
        };

        if(isset($query['start_time']) && $query['start_time']!=''){
            $price->andWhere(['>=','o.add_time',date("Y-m-d 00:00:00", strtotime($query['start_time']))]);
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $price->andWhere(['<=','o.add_time',date("Y-m-d 23:59:59", strtotime($query['end_time']))]);
        }

        $price->orderBy('audit_id desc');
        $count = $price->count();
        $lists = $price->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();


        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/change-price',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getAuditGoodsSku($audit_id,$query = [], $page = 1, $limit = 100){
        $price = Price::find()->where(['audit_id'=>$audit_id])->asArray()->one();
        $skuids = explode(',',$price['skuids']);
        $goods = GoodsSku::find()
            ->select('gs.*,g.yx_goods_id,g.goods_name,c1.category_name as first_category_name,c2.category_name as second_category_name')
            ->from('{{%goods_sku}} gs')
            ->leftJoin('{{%goods}} g','gs.goods_id=g.goods_id')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where(["gs.sku_id"=>$skuids]);
        if(isset($query['spuid'])  && $query['spuid']!=""){
            $goods->andWhere(['yx_goods_id'=>$query['spuid']]);
        };

        if(isset($query['skuid'])  && $query['skuid']!=""){
            $goods->andWhere(['yx_sku_id'=>$query['skuid']]);
        };
        if(isset($query['goods_name']) && $query['goods_name']!=""){
            $goods->andWhere(['LIKE','goods_name',$query['goods_name']]);
        };
        $second_category = '';
        if(isset($query['first_category']) && $query['first_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $second_category = $this->getCategory($query['first_category'],2);
        };

        if(isset($query['second_category']) && $query['second_category']!=0){
            $goods->andWhere(['gc.first_category_id'=>$query['first_category']]);
            $goods->andWhere(['gc.second_category_id'=>$query['second_category']]);
        };

        $goods->orderBy('goods_id desc');
        $count = $goods->count();
        $lists = $goods->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();

        foreach($lists AS $key=>&$value){
            if($price['price_type']==0){
                $value['temp_price'] = $value['yanxuan_price'];
            }else{
                if($price['price_base']==0){
                    if($price['price_model']==0){
                        $value['temp_price'] = $value['yanxuan_price']*(1+$price['price_count']/100);
                    }else{
                        $value['temp_price'] = $value['yanxuan_price']*(1-$price['price_count']/100);
                    }
                }else{
                    if($price['price_model']==0){
                        $value['temp_price'] = $value['channel_price']*(1+$price['price_count']/100);
                    }else{
                        $value['temp_price'] = $value['channel_price']*(1-$price['price_count']/100);
                    }
                }
            }

        }
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/goods-sku-audit',
                'query' => $query
            ],
            'second_category'=>$second_category
        ];
        return $return;
    }

    public function getExportAuditGoodsSku($audit_id){
        $price = Price::find()->where(['audit_id'=>$audit_id])->asArray()->one();
        $skuids = explode(',',$price['skuids']);
         $goods = GoodsSku::find()
            ->select('gs.*,g.yx_goods_id,g.goods_name,c1.category_name as first_category_name,c2.category_name as second_category_name')
            ->from('{{%goods_sku}} gs')
            ->leftJoin('{{%goods}} g','gs.goods_id=g.goods_id')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            ->leftJoin('{{%category}} c1','c1.category_id=gc.first_category_id')
            ->leftJoin('{{%category}} c2','c2.category_id=gc.second_category_id')
            ->where(["gs.sku_id"=>$skuids])->orderBy('goods_id desc')->asArray()->all();

        foreach($goods AS $key=>&$value){
            if($price['price_type']==0){
                $value['temp_price'] = $value['yanxuan_price'];
            }else{
                if($price['price_base']==0){
                    if($price['price_model']==0){
                        $value['temp_price'] = $value['yanxuan_price']*(1+$price['price_count']/100);
                    }else{
                        $value['temp_price'] = $value['yanxuan_price']*(1-$price['price_count']/100);
                    }
                }else{
                    if($price['price_model']==0){
                        $value['temp_price'] = $value['channel_price']*(1+$price['price_count']/100);
                    }else{
                        $value['temp_price'] = $value['channel_price']*(1-$price['price_count']/100);
                    }
                }
            }
        }
        return $goods;
    }

    public function nopass($audit_id){
        $price = Price::findOne($audit_id);
        $price->audit_status = 2;
        $price->audit_time = date('Y-m-d H:i:s');
        return $price->save();
    }

    public function pass($audit_id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $price = Price::findOne($audit_id);
        $price->audit_status = 1;
        $price->audit_time = date('Y-m-d H:i:s');
        $result1 = $price->save();
        $result2 = true;
        $goodsskus = GoodsSku::findAll(['sku_id'=>$price->skuids]);
        foreach($goodsskus AS $key=>$value){
            if($price['price_type']==0){
                $temp_price = $value['yanxuan_price'];
            }else{
                if($price['price_base']==0){
                    if($price['price_model']==0){
                        $temp_price = $value['yanxuan_price']*(1+$price['price_count']/100);
                    }else{
                        $temp_price = $value['yanxuan_price']*(1-$price['price_count']/100);
                    }
                }else{
                    if($price['price_model']==0){
                        $temp_price = $value['channel_price']*(1+$price['price_count']/100);
                    }else{
                        $temp_price = $value['channel_price']*(1-$price['price_count']/100);
                    }
                }
            }
            $value->sku_price = $temp_price;
            $result2 &= $value->save();
        }
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }


}
